#define DEBUG
#include <cstdio>
#define MAXN 100000
#define MAXM 100000

using namespace std;

int min(int a, int b) {
  return a<b ? a : b;
}

class Set {
public:
  int fa[MAXN+1], val[MAXN+1];

  void init(int n) {
    for (int i=1; i<=n; i++) {
      fa[i] = i, val[i] = 0;
    }
  }

  int getFa(int o) {
    if (fa[o]!=o) {
      int temp=getFa(fa[o]);
      val[o] = (val[o]+val[fa[o]])%2;
      fa[o] = temp;
    }
    return fa[o];
  }

  bool merge(int a, int b, int v) {
    int x=getFa(a), y=getFa(b);
    if (x!=y) {
      fa[x] = y;
      val[x] = (val[a]+val[b]+v)%2;
      return true;
    } else {
      if ((val[a]+val[b])%2==v) {
	return true;
      } else {
	return false;
      }
    }
  }
};

int solve(int n, int m, int side[][3], int x) {
  static Set set;
  static int f[MAXN+1][2];
  set.init(n);
  for (int i=1; i<=m; i++) {
    if (!set.merge(side[i][1], side[i][2], side[i][0]!=x)) {
      return -1;
    }
  }
  for (int i=1; i<=n; i++) {
    f[i][0] = f[i][1] = 0;
  }
  for (int i=1; i<=n; i++) {
    set.getFa(i);
    f[set.getFa(i)][set.val[i]]++;
  }
  int ans=0;
  for (int i=1; i<=n; i++) {
    ans += min(f[i][0], f[i][1]);
  }
  return ans;
}

int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  static int side[MAXM+1][3];
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=m; i++) {
    char c;
    scanf("%d %d %c ", side[i]+1, side[i]+2, &c);
    side[i][0] = c=='B';
  }

  int ans=-1;
  for (int i=0; i<=1; i++) {
    int t=solve(n, m, side, i);
    if (ans<0 || (t>=0 && t<ans)) {
      ans = t;
    }
  }
  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
